package com.bipowernode.jdbc;

import java.sql.*;
import java.util.HashMap;
import java.util.Map;
import java.util.ResourceBundle;
import java.util.Scanner;

public class 模拟用户登录 {
    public static void main(String[] args) {
        //初始化界面
        Map<String, String> userLoginInFo= initUI();
        //登录
        boolean loginSuccess = login(userLoginInFo);
        //最后输出结果
        System.out.println(loginSuccess ? "登陆成功" : "登陆失败");
    }

    /**
     * 用户登录功能
     * @param userLoginInFo 登录信息:账户,密码
     * @return false表示登陆失败 true表示登陆成功
     */
    private static boolean login(Map<String, String> userLoginInFo) {
        Connection conn = null;
        PreparedStatement ppst = null;
        ResultSet rs = null;

        Statement pst = null;

        ResourceBundle bundle = ResourceBundle.getBundle("jdbc");
        String driver = bundle.getString("jdbc.driver");
        String url = bundle.getString("jdbc.url");
        String username = bundle.getString("jdbc.username");
        String password = bundle.getString("jdbc.password");

        String loginName = userLoginInFo.get("loginName");
        String loginPwd = userLoginInFo.get("loginPwd");

        boolean loginSuccess = false;

        try {
            //注册驱动
            Class.forName(driver);
            //获取连接
            conn = DriverManager.getConnection(url, username, password);
            //获取数据库对象,执行sql
            String sql = "select * from t_user where loginName = ? and loginPwd = ?";
            ppst = conn.prepareStatement(sql);
            ppst.setString(1,loginName);
            ppst.setString(2,loginPwd);
            /*
            //sql注入
            pst = conn.createStatement();
            String sql = "select * from t_user where " +
                    "loginName = '"+userLoginInFo.get("loginName")+"' and loginPwd = '"+userLoginInFo.get("loginPwd")+"'";
            rs = pst.executeQuery(sql);
            */

            // 处理结果集
            rs = ppst.executeQuery();
            if (rs.next()) {
                loginSuccess = true;
            }
        } catch (Exception e) {
            e.printStackTrace();
        }finally {
            if(rs != null){
                try {
                    rs.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(ppst != null){
                try {
                    ppst.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
            if(conn != null){
                try {
                    conn.close();
                } catch (SQLException e) {
                    e.printStackTrace();
                }
            }
        }
        return loginSuccess;
    }

    /**
     * 初始化界面
     * @return 返回用户登录的用户名和密码
     */
    private static Map<String, String> initUI() {
        Scanner scanner = new Scanner(System.in);
        System.out.print("用户名:");
        String loginName = scanner.nextLine();
        System.out.print("密码:");
        String loginPwd = scanner.nextLine();
        Map<String, String> userLoginInFo = new HashMap<>();
        userLoginInFo.put("loginName", loginName);
        userLoginInFo.put("loginPwd", loginPwd);
        return userLoginInFo;
    }
}
